function permute(input) {
	let result = [],
		usedChars = [];
	function main(input) {
		var i, ch;
		for (i = 0; i < input.length; i++) {
			ch = input.splice(i, 1)[0];
			usedChars.push(ch);
			if (input.length == 0) {
				result.push(usedChars.slice());
			}
			main(input);
			input.splice(i, 0, ch);
			usedChars.pop();
		}
		return result
	}
	return main(input);
};
console.log(permute([2, 1, 3]));
